<?php


namespace app\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class DisconnectCupboard extends Command
{

    protected $hook = [
        // 郑州
        410100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=89a36bc7-0ee4-4aa3-a0f3-9596c7b944af',
        // 西安
        610100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d30c0234-27ed-4114-aa70-14700bd3dfb5',
//        610100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=cfa594a0-7218-4885-876c-33b63ec71192',//测试
        // 成都
        510100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e9256a2f-469e-4fb1-8954-bb47a9c84aad',
        // 武汉
        420100 => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3167a1df-5bc7-4ec5-bb96-0a7115a6f624'
    ];

    protected $region = [
        410100 => "郑州市",
        610100 => "西安市",
        510100 => "成都市",
        420100 => "武汉市"
    ];


    protected function configure()
    {
        // 指令配置
        $this->setName('send_cupboard_disconnect')->setDescription('货柜离线推送');
    }

    protected function execute(Input $input, Output $output)
    {

//        return $output->writeln('暂停执行');
        $franchiserIdArr = Db::connect('cupboard')->table('franchiser')->whereIn("operator_id", [407, 408, 410, 412])->column("id");

        $city = array_values($this->region);
        $list = Db::connect('cupboard')->table('cupboard')
            ->field("city,imei,franchiser_id")
            ->whereIn("city", $city)
            ->whereIn("franchiser_id", $franchiserIdArr)
            ->whereNotNull("group_id")
            ->where("online_status", 0)
            ->select()
            ->toArray();

        $zhengzhou = "";
        $zhengzhou1 = "";
        $zhengzhou2 = "";
        $xian = "";
        $chengdu = "";
        $wuhan = "";
        $i = 0;
        $j = 0;
        $k = 0;
        $l = 0;
        foreach ($list as $value) {
            $franchiserInfo = Db::connect('cupboard')->table('franchiser')->field("phone,name,bdm_id,bd_id")->where('id', $value['franchiser_id'])->find();
            if (!$franchiserInfo) {
                continue;
            }
            $bdmName = Db::name('admin_user')->where('id', $franchiserInfo['bdm_id'])->value("nick") ?: '未知';
            $bdName = Db::name('admin_user')->where('id', $franchiserInfo['bd_id'])->value("nick") ?: '未知';

//            if ($value['city'] == "郑州市") {
//                $i++;
//                if ($i <= 30) {
//                    $zhengzhou .= $i . "、" . $franchiserInfo['name'] . "(" . $franchiserInfo['phone'] . "),设备号" . $value['imei'] . "，离线状态。BDM:" . $bdmName . ",BD:" . $bdName . PHP_EOL;
//                } else if ($i > 30 && $i <= 60) {
//                    $zhengzhou1 .= $i . "、" . $franchiserInfo['name'] . "(" . $franchiserInfo['phone'] . "),设备号" . $value['imei'] . "，离线状态。BDM:" . $bdmName . ",BD:" . $bdName . PHP_EOL;
//                } else {
//                    $zhengzhou2 .= $i . "、" . $franchiserInfo['name'] . "(" . $franchiserInfo['phone'] . "),设备号" . $value['imei'] . "，离线状态。BDM:" . $bdmName . ",BD:" . $bdName . PHP_EOL;
//                }
//            }
            if ($value['city'] == "西安市") {
                $j++;
                $xian .= $j . "、" . $franchiserInfo['name'] . "(" . $franchiserInfo['phone'] . "),设备号" . $value['imei'] . "，离线状态。BDM:" . $bdmName . ",BD:" . $bdName . PHP_EOL;
            }
            if ($value['city'] == "成都市") {
                $k++;
                $chengdu .= $k . "、" . $franchiserInfo['name'] . "(" . $franchiserInfo['phone'] . "),设备号" . $value['imei'] . "，离线状态。BDM:" . $bdmName . ",BD:" . $bdName . PHP_EOL;
            }
            if ($value['city'] == "武汉市") {
                $l++;
                $wuhan .= $l . "、" . $franchiserInfo['name'] . "(" . $franchiserInfo['phone'] . "),设备号" . $value['imei'] . "，离线状态。BDM:" . $bdmName . ",BD:" . $bdName . PHP_EOL;
            }
        }

//        if ($zhengzhou) {
//            $params = [
//                "msgtype" => "text",
//                "text" => [
//                    "content" => $zhengzhou
//                ]
//            ];
//            curl_post($this->hook[410100], json_encode($params));
//        }
//        if ($zhengzhou1) {
//            $params = [
//                "msgtype" => "text",
//                "text" => [
//                    "content" => $zhengzhou1
//                ]
//            ];
//            curl_post($this->hook[410100], json_encode($params));
//        }
//        if ($zhengzhou2) {
//            $params = [
//                "msgtype" => "text",
//                "text" => [
//                    "content" => $zhengzhou2
//                ]
//            ];
//            curl_post($this->hook[410100], json_encode($params));
//        }
        if ($xian) {
            $params = [
                "msgtype" => "text",
                "text" => [
                    "content" => $xian
                ]
            ];
            curl_post($this->hook[610100], json_encode($params));
        }
        if ($chengdu) {
            $params = [
                "msgtype" => "text",
                "text" => [
                    "content" => $chengdu
                ]
            ];
            curl_post($this->hook[510100], json_encode($params));
        }
        if ($wuhan) {
            $params = [
                "msgtype" => "text",
                "text" => [
                    "content" => $wuhan
                ]
            ];
            curl_post($this->hook[420100], json_encode($params));
        }
    }
}